/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#ifndef DEVMON_CONTEXT_H
#define DEVMON_CONTEXT_H

#include <mc/dict.h>
#include <mc/engine.h>
#include <mc/exception.h>
#include <mc/reflect.h>

#include <vector>

#include "device/driver.h"
#include <mc/expr/function/call.h>

namespace devmon {

struct unit {
    MC_REFLECTABLE("Unit")

    void           validate_unit();
    device_driver& load_compatible_driver();

    std::string              m_type;
    std::string              m_name;
    std::vector<std::string> m_compatible;
};

struct connector {
    MC_REFLECTABLE("Connector")

    void validate_connector();

    uint8_t      m_system_id{1};
    std::string  m_position;
    uint8_t      m_slot{0};
    std::string  m_group_position;
    std::string  m_group_id;
    std::string  m_chassis_id;
    std::string  m_manager_id;
    std::string  m_container;
    std::string  m_silk_text;
    mc::variants m_buses;
};

class device_csr {
public:
    MC_REFLECTABLE("DeviceCsr")

    void validate_csr();
    std::pair<mc::engine::abstract_object*, device_driver*>
         create_devices_object(mc::engine::service* service);
    void finalize_device_setup(mc::engine::service*         service,
                               mc::engine::abstract_object* device_object, device_driver& drv);
    void remove_devices_object(mc::engine::service* service);
    void init_functions(mc::engine::service* service);
    void init(const mc::variant& csr, mc::dict connector);

    // 获取连接器信息
    const connector& get_connector() const {
        return m_connector;
    }

    // 处理拓扑对象，返回包含拓扑、对象和JSON字符串的字典
    mc::dict process_topology_object();

    // 测试用方法：获取处理后的对象数据
    mc::dict get_processed_objects() const;

private:
    void create_child_device(mc::engine::service* service, std::shared_ptr<device_driver_t> device);
    void create_childs_device(mc::engine::service* service, device_driver& drv);
    mc::engine::abstract_object* create_component_device(mc::engine::service* service,
                                                         device_driver&       drv);
    void                         create_common_devices(mc::engine::service* service);
    mc::engine::abstract_object* find_parent_object(mc::engine::service* service, mc::dict& object);
    bool                         is_function_object(const std::string& object_name);
    mc::mutable_dict             process_function_args(const mc::expr::func& func_obj);

    // 对整个objects进行占位符替换
    void process_objects_placeholders();

    // 拓扑处理的辅助方法
    std::map<std::string, std::string> build_bus_mapping();
    std::map<std::string, std::string>
    collect_and_create_object_mapping(const std::map<std::string, std::string>& bus_mapping);
    mc::mutable_dict
    extract_and_rename_objects(const std::map<std::string, std::string>& object_name_mapping);
    mc::mutable_dict convert_topology_to_simplified_format(
        const std::map<std::string, std::string>& bus_mapping,
        const std::map<std::string, std::string>& object_name_mapping);

private:
    friend struct mc::reflect::reflector<device_csr>;

    unit             m_unit;
    mc::mutable_dict m_objects;
    mc::mutable_dict m_topology;
    std::string      m_topology_json;

    connector m_connector;
    mc::dict  m_original_connector;
};

} // namespace devmon

#endif // DEVMON_CONTEXT_H